//
//  PHXQViewController.m
//  LastTemp
//
//  Created by apple on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PHXQViewController.h"
#import "AFSoundManager.h"
#import "BassModel.h"
#import "bofangViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface PHXQViewController ()
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)NSMutableArray *playHomeArr;
@property(nonatomic,retain)UIActivityIndicatorView *activity;
@end

@implementation PHXQViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
       
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self urlData1];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 150)];
    UIImageView *backGround = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    backGround.alpha = 0.6;
    [myView addSubview:backGround];
    [backGround release];
    backGround.image = [UIImage imageNamed:@"once2"];
    
    
    
    UIImageView *MyimageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 25, 100, 100)];
    [MyimageView sd_setImageWithURL:[NSURL URLWithString:self.pic_url]];
    [myView addSubview:MyimageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, self.view.frame.size.width - 120, 100)];
    label.text = self.desc;
    label.numberOfLines = 0;
    [label sizeToFit];
    [myView addSubview:label];
    self.tableView.tableHeaderView = myView;
    [_tableView release];
    [MyimageView release];
    [myView release];
    [label release];
    
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(WIDTH/2, 100, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.arr.count > 0) {
        [self.activity stopAnimating];
    }
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    PHXQCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[PHXQCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:str] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    PHXQModel *model = self.arr[indexPath.row];
    cell.nameLabel.text = model.song_name;
    cell.singerLabel.text = model.singer_name;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (void)dealloc
{
    [_arr release];
    [_tableView release];
    [super dealloc];
}
//网络请求
-(void)urlData1
{
    if ([AVSingleton shareSingleton].internetNum == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    } else {
    NSString *str = [NSString stringWithFormat:@"http://iting.music.189.cn:9200/iting2/imusic/V2?ids=%@_5_1_10&protocolCode=2.0&method=get_catalog_res_list&sid=234472872&networkType=wifi&serviceCode=100&sessionId=83356C7DD4CDA5C8EE072C41746AA3A6&format=json&timestamp=20150424142339&sign=7592df598ca12419f87a8af3be814ec9&imsi=87D618C0-A675-458D-BB55-BED13C712771&company=&appChannelCode=10000000&appVerCode=2.000.004",self.resid];
    NSString *strEncode = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strEncode];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *catalogResListArr = [dic objectForKey:@"catalogResList"];
        for (NSMutableArray *cataArr in catalogResListArr) {
            for (NSMutableDictionary *cataDic in cataArr) {
                PHXQModel *phModel = [[PHXQModel alloc] init];
                [phModel setValuesForKeysWithDictionary:cataDic];
                [self.arr addObject:phModel];
                [phModel release];
            }
        }
        [self.tableView reloadData];
    }];
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     self.playHomeArr = [NSMutableArray array];
    PHXQModel *model = self.arr[indexPath.row];
    NSMutableArray *qq_url_listArr = [model.qqInfo objectForKey:@"qq_url_list"];
    NSMutableDictionary *dic = [qq_url_listArr firstObject];
    
    
    
    if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
        [[AFSoundManager sharedManager] stop];
    }
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:dic[@"url"] andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
        [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
        
        
    }];
    
    for (NSInteger i = 0; i < self.arr.count; i ++) {
        PHXQModel *model = self.arr[i];
        BassModel *base = [[BassModel alloc] init];
        base.name1 = model.song_name;
        base.singerName1 = model.singer_name;
        NSMutableArray *qq_url_listArr = [model.qqInfo objectForKey:@"qq_url_list"];
        NSMutableDictionary *dic = [qq_url_listArr firstObject];
        base.url1 = dic[@"url"];
        [self.playHomeArr addObject:base];
        [base release];
    }
    
    [AVSingleton shareSingleton].fromArr = self.playHomeArr;
    [AVSingleton shareSingleton].fromNum = indexPath.row;
    [AVSingleton shareSingleton].myBool = YES;

    bofangViewController *play = [[bofangViewController alloc] init];
    [self presentViewController:play animated:YES completion:^{
        
    }];
    [play release];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
